﻿CREATE PROCEDURE P_GetEnterprises
	@pIdentificationTypeCode VARCHAR (3) = NULL
	,@pIdentificationTypeNumber VARCHAR (50) = NULL
	,@pSearchTerms VARCHAR (50) = NULL
AS

	SELECT 
		E.RazonSocial				Ent_Name
		,E.CodTipoIdentificacion	Ent_IdentificationType_Code
		,E.NumeroIdentificacion		Ent_IdentificationNumber
		,E.NombreRepresentanteLegal	Ent_LegalRepresentative
		,E.DigitoVerificacion		Ent_IdentificationVerificationDigit
		,S.Email					Ent_Email
	FROM
		TEntidades E
	INNER JOIN
		TSucursales S ON 
			E.CodEntidad = S.CodEntidad
			AND S.CodSucursal = '00000'
	WHERE
		(@pIdentificationTypeCode IS NULL OR E.CodTipoIdentificacion = @pIdentificationTypeCode)
		AND (@pIdentificationTypeNumber IS NULL OR E.NumeroIdentificacion = @pIdentificationTypeNumber)
		AND (@pSearchTerms IS NULL OR (
			E.RazonSocial LIKE @pSearchTerms 
			OR E.NumeroIdentificacion LIKE @pSearchTerms
		))
	ORDER BY
		E.RazonSocial